
let fs = require('fs');

let path = require('path')
let rs = fs.createReadStream(path.resolve(__dirname,'name.txt'),{
  flags:'r',//r w
  highWaterMark:5,
  encoding:'utf8',//默认null buffer
  autoClose:true,
  start:0,
  // end:5,//slice(start,end)
});
let ws = fs.createWriteStream(path.resolve(__dirname,'name1.txt'),{
  flag:'w',
  encoding:'utf8',
  highWaterMark:5,
  autoClose: true,
  start:0,
});


function pipe(r, w) {
  r.on('data',(data)=>{
    let flag = w.write(data);
    if (!flag) r.pause();
  })

  w.on('drain',()=>{
    console.log('抽干');
    r.resume();
  })

  r.on('end',()=>{
    console.log('文件读取完毕')
    w.end('结束')
  })
}

// pipe(rs,ws)

rs.pipe(ws);
